//
//  TabBarViewController.swift
//  XXSwiftDemo
//
//  Created by admin on 2022/5/19.
//

import UIKit

struct TabItem{
    
    var title:String
    var image:String
    var selectImage:String
    var type:UIViewController.Type
    
    init(_ title:String, _ image:String, _ selectImage:String, _ type:UIViewController.Type) {
        self.title = title
        self.image = image
        self.selectImage = selectImage
        self.type = type
    }
}

class XXBaseTabBarViewController: UITabBarController {

    var items:Array<TabItem> = []
    var normalColor = UIColor.gray
    var selectColor = UIColor.blue
    var font:UIFont = UIFont.systemFont(ofSize: 12)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        config()
        for item in items{
            addChild(item)
        }
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : normalColor,
             NSAttributedString.Key.font : font], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : selectColor,
             NSAttributedString.Key.font : font], for: .selected)
        UITabBar.appearance().barStyle = .default
        UITabBar.appearance().isTranslucent = false
        UITabBar.appearance().barTintColor = .white
    }
    func config(){
        normalColor = .x333
        selectColor = .hex("#4894FF")
        items = [TabItem.init("111", "", "", HomeViewController.self),
                 TabItem.init("222", "", "", HomeViewController.self),
            ]
    }
    func addChild(_ item:TabItem) {
        let vc = UINavigationController.init(rootViewController:item.type.init())
        vc.tabBarItem.title = item.title
        vc.tabBarItem.image = UIImage(named: item.image)
        vc.tabBarItem.selectedImage = UIImage(named: item.selectImage)
        addChild(vc)
    }

}
